import indexDBStore from "@/stores/indexDBStore";
import { arrayBufferToURL, arrayBufferToBase64, readFileAsArrayBuffer, base64ToArrayBuffer } from "@/utils/fileUtil"
const indexedDB = indexDBStore()

export async function getInterface() {
    //初始化form
    const formStr = localStorage.getItem('InterfaceStore')
    return typeof formStr !== 'string' || formStr.length === 0 ? null : JSON.parse(formStr)
}

export async function saveInterface(data) {
    localStorage.setItem('InterfaceStore', JSON.stringify(data))
}

export function listBackgroundImage() {
    return new Promise((resolve, reject) => {
        indexedDB.getList("images", function (res) {
            let list = []
            for (const img of res) {
                if (img) {
                    list.push({
                        id: img.id,
                        url: arrayBufferToURL(img.data, 'image/png')
                    })
                }
            }
            resolve(list)
        }, function () {
            reject()
        })
    })
}

export function exportBackgroundImage() {
    return new Promise((resolve, reject) => {
        indexedDB.getList("images", function (res) {
            let list = []
            for (const img of res) {
                if (img) {
                    list.push({
                        id: img.id,
                        url: arrayBufferToBase64(img.data)
                    })
                }
            }
            resolve(list)
        }, function () {
            reject()
        })
    })
}

export function getBackgroundImage(id) {
    return new Promise((resolve, reject) => {
        indexedDB.get("images", id, function (res) {
            if(!res) {
                resolve({})
                return
            }
            resolve({
                id: res.id,
                url: arrayBufferToURL(res.data, 'image/png')
            })
        }, function () {
            reject()
        })
    })
}

export function importBackgroundImage(data) {
    return new Promise((resolve, reject) => {
        try {
            indexedDB.add("images", { id: data.id, data: base64ToArrayBuffer(data.url) }, function () {
                resolve()
            }, function () {
                reject()
            })
        } catch (e) {
            ElMessage.error('文件内容不正确')
        }
    })
}

export function addBackgroundImage(file) {
    return new Promise((resolve, reject) => {
        readFileAsArrayBuffer(file).then(arrayBuffer => {
            try {
                indexedDB.add("images", { id: Date.now().toString(), data: arrayBuffer }, function () {
                    resolve()
                }, function () {
                    reject()
                })
            } catch (e) {
                ElMessage.error('文件内容不正确')
            }
        }).catch(() => {
            reject()
        })
    })
}

export function delBackgroundImage(id) {
    return new Promise((resolve, reject) => {
        indexedDB.del("images", id, function () {
            resolve()
        }, function () {
            reject()
        })
    })
}